package com.SFA.OrderManagement;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.FrameWork.Common.AbstractBaseActivity;
import com.FrameWork.Common.CommonFunction;
import com.FrameWork.Entity.CustomerInfoEntity;
import com.FrameWork.Entity.OrderEntity;
import com.FrameWork.Entity.OrderLine;
import com.FrameWork.Utils.DateTimeUtils;
import com.SFA.R;

public class OrderDetailActivity extends AbstractBaseActivity {

	private MenuItem shoppingcart = null;
	private MenuItem print = null;
	private OrderHeaderFragment headerFragment = null;
	private OrderEntity entity = null;
	private OrderLineFragment linefragment = null;
	private CustomerInfoEntity customer = null;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Builder builder = new AlertDialog.Builder(this)
					.setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle("确定要放弃当前的编辑，返回吗?")
					.setPositiveButton(R.string.RETURN,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									finish();
								}

							}).setNegativeButton(R.string.CANCEL, null);
			builder.show();

		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.orderdetailactivity);
		linefragment = (OrderLineFragment) getFragmentManager()
				.findFragmentById(R.id.frag_line);
		headerFragment = (OrderHeaderFragment) getFragmentManager()
				.findFragmentById(R.id.frag_header);

		ActionBar bar = getActionBar();
		bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
				| ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE);
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		bar.setDisplayShowHomeEnabled(true);
		Intent intent = getIntent();
		entity = (OrderEntity) intent.getSerializableExtra("ORDER");
		customer = (CustomerInfoEntity) intent.getSerializableExtra("CUSTOMER");

		if (entity == null) {
			entity = new OrderEntity();
			entity.ORDER_NO = new OrderManager().GenertorOrderNo(this);
			entity.CUSTOMER_ID = customer.ID;
			entity.ORDER_DATE = DateTimeUtils.GetCurrentTime();
		}

		bar.setTitle("订单编号：" + entity.ORDER_NO);
		if (entity.Lines == null) {
			entity.Lines = new ArrayList<OrderLine>();
		}

		linefragment.setData(entity, customer);
		if (intent.getSerializableExtra("DATA") != null)
			linefragment.SetProductList(intent);
		headerFragment.LoadData(entity);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		shoppingcart = menu.add("提交订单");
		shoppingcart.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		if (entity.ID != null) {
			print = menu.add("打印");
			print.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item == shoppingcart) {
			if (!linefragment.Check()) {

				return false;
			}
			// 先判断是否都输入正确
			entity = linefragment.getEntity();
			// headerFragment.toSaveMethod();
		} else if (item == print) {
			dialog = CommonFunction.ShowProgressDialog(context, "正在提交订单打印,请稍后");
			dialog.show();
			new Thread(new Runnable() {

				@Override
				public void run() {
					OrderManager manager = new OrderManager();
					boolean success = manager.PrintOrder(context, entity,
							getString(R.string.MSG_ORDER_001));

					String str = context.getString(R.string.MSG_ORDER_002);
					if (!success) {
						str = manager.getErrorMessage();
					}
					else
					{
						str = "打印成功";
					}
					Message msg = new Message();
					msg.obj = str;
					handler.sendMessage(msg);
				}

			}).start();
		}
		return true;
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
			if(msg.obj != null)
				CommonFunction.ShowDialog(context, msg.obj.toString());
		}
	};

}
